<?php
include('header.php');
include('geocoder.php');
/* 
 NEW_ORG_REGISTER.PHP
 Allows user to create a new organizaton in the database
*/
 
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($orgName, $firstName, $lastName, $areaCode, $prefix, $lineNumber, $faxAreaCode, $faxPrefix, $faxLineNumber, $streetAddress, $streetAddress2, $city, $state, $zipCode, $websiteAddress, $email, $times, $additionalInfo, $category, $error)
{
	// if there are any errors, display them
 	if ($error != '')
 	{
 		echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 	}?> 
 	<div id="form_container">
	<form id="form" class="appnitro" method="post" action="">
    <div class="form_description">
	<h2>Organization</h2>
	<p>Add a New Organization to the System</p>
	</div>
    	<ul>
        	<div>* Required</div>
    		<!--Orgnanization Name-->
            <li><label class="description">Organization Name *</label>
       		<span>
        		<input class="element text" type="text" name="orgName" value=""/>
        	</span>
        	</li>
            
            <!--Contact Information-->
            <li><label class="description">Contact Information *</label>
            
            <!-- First Name -->
			<span>
				<input name="firstName" class="element text" maxlength="255" size="8" value="" />
				<label>First Name *</label>
			</span>
		
        <!-- Last Name -->
			<span>
				<input name="lastName" class="element text" maxlength="255" size="14" value="" />
				<label>Last Name *</label>
			</span>
            </li>
            
            <!-- Email -->
            <li>
                <span>
                    <input name="email" class="element text large" type="text" maxlength="255" value="" />
                    <label>Email Address</label>
                </span>
            </li>
            
            <!--Phone and Fax-->
            <li>
			<span>
				<input name="areaCode" class="element text" size="3" maxlength="3" value="" type="text"> -
				<label>Phone *</label>
			</span>
			<span>
				<input name="prefix" class="element text" size="3" maxlength="3" value="" type="text"> -
			</span>
			<span>
				<input name="lineNumber" class="element text" size="4"maxlength="4"value="" type="text">
			</span>
            </li>
            <li>
            <span>
				<input name="faxAreaCode" class="element text" size="3" maxlength="3" value="" type="text"> -
				<label>Fax</label>
			</span>
			<span>
				<input name="faxPrefix" class="element text" size="3" maxlength="3" value="" type="text"> -
			</span>
			<span>
				<input name="faxLineNumber" class="element text" size="4" maxlength="4"value="" type="text">
			</span>
			</li>
            
            <!-- Address -->
			<li><label class="description">Address *</label>
			<span>
				<input name="streetAddress" class="element text" value="" type="text">
				<label>Street Address *</label>
            </span>
            </li>
            <li>
            <span>
                <input name="streetAddress2" class="element text" value="" type="text">
                <label>Address Line 2</label>
            </span>
            </li>
            <li>
			<span>
				<input name="city" class="element text" size="10" maxlength="30" value="" type="text"> -
				<label>city *</label>
			</span>
			<span>
				<input name="state" class="element text" size="3" maxlength="15" value="" type="text"> -
                <label>state *</label>
			</span>
			<span>
				<input name="zipCode" class="element text" size="4"maxlength="15"value="" type="text">
                <label>Zip Code *</label>
			</span>
            </li>
            
            <!--Website Address-->
            <li><label class="description">Website Address</label>
            <span>
            	<input class="element text" name="websiteAddress" value="" type="text" />
            </span>
            </li>            
            
            <!-- Organization type -->
            <li>
                <label class="description">Organization Type *</label>
                <div>
                <select name="category">
                    <option value="farm">Farm</option>
                    <option value="farmers_market">Farmer's Market</option>
                    <option value="community_garden">Community Garden</option>
                    <option value="orchard">Orchard</option>
                    <option value="csa">CSA</option>
                    <option value="other">Other</option>
                </select>
                 </div>
            </li>
            
            <!-- times-->
            <li><label class="description" >Times available *</label>
            <div>
                <textarea name="times" class="element textarea medium"></textarea>
            </div>
            <p class="guidelines">
            <small>
                    Please enter the days of the week and times you are avaialable.
            </small>
            </p>
        	</li>
          	
            <!-- Additional Information -->
            <li><label class="description">Additional information </label>
                <div>
                    <textarea name="additionalInfo" class="element textarea medium"></textarea>
                </div>
                <p class="guidelines">
                    <small>
                        Is there anything other information you would like to add?
                    </small>
                </p>
            </li>

    		<li class="button">
        		<input type="submit" name="submit" value="Submit">
        	</li>
		</ul>
	</form> 
    </div>
<?php 
include('footer.php');
}

// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{ 
	// get form data, making sure it is valid
	$orgName = mysql_real_escape_string($_POST['orgName']);
	$firstName = mysql_real_escape_string($_POST['firstName']);
	$lastName = mysql_real_escape_string($_POST['lastName']);
	$areaCode = mysql_real_escape_string($_POST['areaCode']);
	$prefix = mysql_real_escape_string($_POST['prefix']);
	$lineNumber = mysql_real_escape_string($_POST['lineNumber']);
	$faxAreaCode = mysql_real_escape_string($_POST['faxAreaCode']);
	$faxPrefix = mysql_real_escape_string($_POST['faxPrefix']);
	$faxLineNumber = mysql_real_escape_string($_POST['faxLineNumber']);
	$streetAddress = mysql_real_escape_string($_POST['streetAddress']);
	$streetAddress2 = mysql_real_escape_string($_POST['streetAddress2']);
	$city = mysql_real_escape_string($_POST['city']);
	$state = mysql_real_escape_string($_POST['state']);
	$zipCode = mysql_real_escape_string($_POST['zipCode']);
	$websiteAddress = mysql_real_escape_string($_POST['websiteAddress']);
	$email = mysql_real_escape_string($_POST['email']);
	$times = mysql_real_escape_string($_POST['times']);
	$additionalInfo = mysql_real_escape_string($_POST['additionalInfo']);
	$category = mysql_real_escape_string($_POST['category']);
	//geocode function
	$address = $streetAddress . " " . $streetAddress2 . " " . $zipCode;
			$geocodeAddress = preg_replace ( '/\s+/', '+', $address );
			$geocode = geocode ( $geocodeAddress );
			$lat = $geocode ["lat"];
			$lng = $geocode ["lng"];
 
	// check to make sure all fields are entered
	if ($orgName == '' || $firstName == '' || $lastName == '' || $areaCode == '' || $prefix == '' || $lineNumber == '' || $streetAddress == '' || $city == '' || $state == '' || $zipCode == '' || $category == '' || $times == '')
	{
		// generate error message
		$error = 'ERROR: Please fill in all required fields!';
 
		// if either field is blank, display the form again
		renderForm($orgName, $firstName, $lastName, $areaCode, $prefix, $lineNumber, $faxAreaCode, $faxPrefix, $faxLineNumber, $streetAddress, $streetAddress2, $city, $state, $zipCode, $websiteAddress, $email, $times, $additionalInfo, $category, $error);
	}
	else
	{
		// save the data to the database
		mysql_query("INSERT organizations 
		SET 
		orgName = '$orgName', 
		firstName = '$firstName', 
		lastName = '$lastName',
		areaCode = '$areaCode', 
		prefix = '$prefix', 
		lineNumber = '$lineNumber',
		faxAreaCode = '$faxAreaCode',
		faxPrefix = '$faxPrefix',
		faxLineNumber = '$faxLineNumber',
		streetAddress = '$streetAddress', 
		streetAddress2 = '$streetAddress2', 
		city = '$city', 
		state = '$state', 
		zipCode = '$zipCode',
		websiteAddress = '$websiteAddress',
		email = '$email',
		times = '$times',
		additionalInfo = '$additionalInfo',
		lat = '$lat',
		lng = '$lng',
		category = '$category'")
		or die(mysql_error()); 

		// once saved, redirect back to the view page
		header("Location: org_list.php"); 
	}
}
else
// if the form hasn't been submitted, display the form
{
	renderForm('','','','','','','','','','','','','','','','','','','','');
}
?> 